<template>
  <CenterComp>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="ruleForm.username" @blur="chooseName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="telephone">
        <el-input v-model="ruleForm.telephone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="注册身份" prop="privilege">
        <el-select v-model="ruleForm.privilege" placeholder="请选择注册身份">
          <el-option label="普通用户" value="0"></el-option>
          <el-option label="管理员" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
            label="头像"
            placeholder="请上传头像"
          >
            <el-upload
              class="avatar-uploader"
              action="/api/users/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </CenterComp>
</template>

<script>
// import { getUserLogin } from "@/services/movieService";
import CenterComp from "@/components/CenterComp"; //固定路径
export default {
  components: {
    CenterComp,
  },
  data() {
    var validatePass = (rule, value, callback) => {
      const reg = /^\w{6,12}$/;
      if (value == "" || value == undefined || value == null) {
        callback();
      } else {
        if (!reg.test(value) && value != "") {
          callback(new Error("请输入符合条件的密码"));
        } else {
          callback();
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var checkTelephone = (rule, value, callback) => {
      const reg = /^[1][3-9][0-9]{9}$/;
      if (value == "" || value == undefined || value == null) {
        callback();
      } else {
        if (!reg.test(value) && value != "") {
          callback(new Error("请输入符合条件的手机号码"));
        } else {
          callback();
        }
      }
    };
    var checkEmail = (rule, value, callback) => {
      const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (value == "" || value == undefined || value == null) {
        callback();
      } else {
        if (!reg.test(value)) {
          callback(new Error("请输入符合条件的邮箱"));
        } else {
          callback();
        }
      }
    };
    return {
      imageUrl:"",
      ruleForm: {
        username: "",
        password: "",
        checkPass: "",
        telephone: "",
        email: "",
        privilege: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            validator: validatePass,
            trigger: "blur",
          },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        telephone: [
          {
            required: true,
            validator: checkTelephone,
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            validator: checkEmail,
            trigger: "blur",
          },
        ],
        privilege: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        createTime: [
          {
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //上传头像
     handleAvatarSuccess(res, file) {
      console.log(res);
      console.log(URL.createObjectURL(file.raw));
      this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.picture = res.data;
    },
    //上传头像限制
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    chooseName(event) {
      //判断用户名是否重复
      console.log(event.target.value);
      this.$store
        .dispatch("userStore/repeat", { name: event.target.value })
        .then((resp) => {
          console.log("resp.status", resp.status);
          if (resp.status == 0) {
            alert("该用户名已存在!");
          }
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        
        if (valid) {
          //调用repeat函数 传入实参payload
          this.$store.dispatch("userStore/getUsers", this.ruleForm);
          console.log("this.ruleForm", this.ruleForm);
           alert("注册成功!");
          this.$router.push("/login");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
  border: 1px dashed #d9d9d9 !important;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>